<?php


namespace apps\marriage\app\op\controller;


use apps\marriage\core\logic\FileLogic;
use apps\marriage\core\model\MemberAuth;
use apps\marriage\core\model\MemberImages;
use apps\marriage\core\model\MemberProfile;
use apps\marriage\core\model\User;
use apps\marriage\core\service\MemberAuthService;
use apps\marriage\core\service\MemberImagesService;
use apps\marriage\core\service\MemberProfileService;
use apps\marriage\core\service\UserService;
use think\Request;

class MemberAuthController extends BaseMarriageOpController
{
    /**
     * @var MemberAuthService
     */
    private $memberAuthService;

    /**
     * @var UserService
     */
    private $userService;

    /**
     * @var MemberProfileService
     */
    private $memberProfileService;

    /**
     * @var MemberImagesService
     */
    private $memberImagesService;

    protected function init()
    {
        parent::init();
        $this->memberAuthService = service('MemberAuth', SERVICE_NAMESPACE);
        $this->userService = service('User', SERVICE_NAMESPACE);
        $this->memberProfileService = service('MemberProfile', SERVICE_NAMESPACE);
        $this->memberImagesService = service('MemberImages', SERVICE_NAMESPACE);
    }

    /**
     * @return FileLogic
     */
    private function getFileLogic()
    {
        return logic('File', LOGIC_NAMESPACE);
    }

    public function lists(Request $request)
    {
        $pageSize = $request->param('page_size', 10);
        $type = $request->param('type', 'marriage');
        $params = [];
        if($type == 'marriage') {
            $params['marriage_auth_status'] = MemberAuth::AUTH_STATUS_WAIT;
        } else if($type == 'education') {
            $params['education_auth_status'] = MemberAuth::AUTH_STATUS_WAIT;
        } else if($type == 'house') {
            $params['house_auth_status'] = MemberAuth::AUTH_STATUS_WAIT;
        } else if($type == 'car') {
            $params['car_auth_status'] = MemberAuth::AUTH_STATUS_WAIT;
        }
        $res = $this->memberAuthService->pageListByParams($params, $pageSize);
        if(!empty($res['data'])) {
            foreach ($res['data'] as &$rs) {
                $rs = $this->memberAuthService->format($rs);
                $rs['user'] = $this->userService->getByPk($rs['user_id']);
            }
        }
        return $this->success($res);
    }

    public function approve(Request $request)
    {
        $type = $request->param('type', 'marriage');
        $id = $request->param('id', 0, 'intval');
        $params = [
            'id' => $id
        ];
        if($type == 'marriage') {
            $params['marriage_auth_status'] = MemberAuth::AUTH_STATUS_APPROVE;
            $params['marriage_auth_audit_time'] = time();
        } else if($type == 'education') {
            $params['education_auth_status'] = MemberAuth::AUTH_STATUS_APPROVE;
            $params['education_auth_audit_time'] = time();
        } else if($type == 'house') {
            $params['house_auth_status'] = MemberAuth::AUTH_STATUS_APPROVE;
            $params['house_auth_audit_time'] = time();
        } else if($type == 'car') {
            $params['car_auth_status'] = MemberAuth::AUTH_STATUS_APPROVE;
            $params['car_auth_audit_time'] = time();
        }
        $this->memberAuthService->updateByPk($params);
        return $this->success();
    }

    public function reject(Request $request)
    {
        $type = $request->param('type', 'marriage');
        $id = $request->param('id', 0, 'intval');
        $params = [
            'id' => $id
        ];
        if($type == 'marriage') {
            $params['marriage_auth_status'] = MemberAuth::AUTH_STATUS_REJECT;
            $params['marriage_auth_audit_time'] = time();
        } else if($type == 'education') {
            $params['education_auth_status'] = MemberAuth::AUTH_STATUS_REJECT;
            $params['education_auth_audit_time'] = time();
        } else if($type == 'house') {
            $params['house_auth_status'] = MemberAuth::AUTH_STATUS_REJECT;
            $params['house_auth_audit_time'] = time();
        } else if($type == 'car') {
            $params['car_auth_status'] = MemberAuth::AUTH_STATUS_REJECT;
            $params['car_auth_audit_time'] = time();
        }
        $this->memberAuthService->updateByPk($params);
        return $this->success();
    }

    public function user_avatar_list(Request $request)
    {
        $pageSize = $request->param('page_size', 10);
        $params = [
            'avatar_audit_status' => User::AVATAR_AUDIT_STATUS_AUDITING
        ];
        $res = $this->userService->pageListByParams($params, $pageSize);
        $dataList = [];
        foreach ($res['data'] as $rs) {
            $rs = arrayOnly($rs, ['id', 'nick', 'avatar', 'auditing_avatar', 'avatar_audit_status']);
            $rs['avatar_url'] = $this->userService->getAvatarUrl($rs['avatar'], $rs['id']);
            $rs['auditing_avatar_url'] = $this->userService->getAvatarUrl($rs['auditing_avatar'], $rs['id']);
            $memberAuth = $this->memberAuthService->getByUserId($rs['id']);
            $rs['idcard_front_img'] = $memberAuth['idcard_ocr_front'];
            $dataList[] = $rs;
        }
        $res['data'] = $dataList;
        return $this->success($res);
    }

    public function approve_user_avatar(Request $request)
    {
        $userId = $request->param('user_id', 0, 'intval');
        $this->memberProfileService->onAuditUserAvatar($userId, User::AVATAR_AUDIT_STATUS_APPROVED);
        return $this->success();
    }

    public function reject_user_avatar(Request $request)
    {
        $userId = $request->param('user_id', 0, 'intval');
        $this->memberProfileService->onAuditUserAvatar($userId, User::AVATAR_AUDIT_STATUS_REJECT);
        return $this->success();
    }

    public function member_photo_list(Request $request)
    {
        $pageSize = $request->param('page_size', 10);
        $params = [
            'photo_audit_status' => MemberProfile::PHOTO_AUDIT_STATUS_AUDITING
        ];
        $res = $this->memberProfileService->pageListByParams($params, $pageSize);
        $dataList = [];
        foreach ($res['data'] as $rs) {
            $rs = $this->memberProfileService->format($rs);
            $rs = arrayOnly($rs, ['id', 'user_id', 'photo', 'photo_url', 'auditing_photo', 'auditing_photo_url', 'photo_audit_status']);
            $memberAuth = $this->memberAuthService->getByUserId($rs['user_id']);
            $rs['idcard_front_img'] = $memberAuth['idcard_ocr_front'];
            $user = $this->userService->getByPk($rs['user_id']);
            $user = arrayOnly($user, ['id', 'nick', 'avatar_url']);
            $rs['user'] = $user;
            $dataList[] = $rs;
        }
        $res['data'] = $dataList;
        return $this->success($res);
    }

    public function approve_member_photo(Request $request)
    {
        $memberId = $request->param('member_id', 0, 'intval');
        $this->memberProfileService->onAuditPhoto($memberId, MemberProfile::PHOTO_AUDIT_STATUS_APPROVED);
        return $this->success();
    }

    public function reject_member_photo(Request $request)
    {
        $memberId = $request->param('member_id', 0, 'intval');
        $this->memberProfileService->onAuditPhoto($memberId, MemberProfile::PHOTO_AUDIT_STATUS_REJECT);
        return $this->success();
    }

    public function member_image_list(Request $request)
    {
        $pageSize = $request->param('page_size', 10);
        $params = [
            'status' => MemberImages::STATUS_AUDITING
        ];
        $res = $this->memberImagesService->pageListByParams($params, $pageSize);
        $dataList = [];
        foreach ($res['data'] as $rs) {
            $file = $this->getFileLogic()->file($rs['file_id']);
            $rs['file_url'] = $file['url'];
            $memberAuth = $this->memberAuthService->getByUserId($rs['user_id']);
            $rs['idcard_front_img'] = $memberAuth['idcard_ocr_front'];
            $user = $this->userService->getByPk($rs['user_id']);
            $user = arrayOnly($user, ['id', 'nick', 'avatar_url']);
            $rs['user'] = $user;
            $dataList[] = $rs;
        }
        $res['data'] = $dataList;
        return $this->success($res);
    }

    public function approve_member_image(Request $request)
    {
        $imageId = $request->param('image_id', 0, 'intval');
        $this->memberImagesService->updateByPk([
            'id' => $imageId,
            'status' => MemberImages::STATUS_APPROVED
        ]);
        return $this->success();
    }

    public function reject_member_image(Request $request)
    {
        $imageId = $request->param('image_id', 0, 'intval');
        $this->memberImagesService->updateByPk([
            'id' => $imageId,
            'status' => MemberImages::STATUS_REJECT
        ]);
        return $this->success();
    }
}